<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\helper\Time;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Permanent extends Ques
{

    /**
     * Teacher模型对象
     * @var \app\admin\model\Teacher
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Teacher;

    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 统计未完成
     */
    public function ana(){

        $whereQues = [];
        $whereTea = [];
        $where_ = [];
        $where_time = [];
        $filter = json_decode(input('filter'),true);
        $timeArr = explode(' - ',$filter['create_time']);
//            var_dump($timeArr);
        if(isset($timeArr[1])){
            $timestampArr = [
                strtotime($timeArr[0]),strtotime($timeArr[1])
            ];
            $where_['a.create_time'] = ['<',strtotime($timeArr[0])];
            $where_time['a.create_time'] = ['<',strtotime($timeArr[0])];
        }else{
            $timestampArr = Time::today();
            $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
            $where_['a.create_time'] = ['<',$beginToday];
            $where_time['a.create_time'] = ['<',$beginToday];
        }




        if(isset($this -> sess['teacherIds'])){
            $whereQues['teacher_id'] = ['in',$this -> sess['teacherIds']];
            $whereTea['a.id'] = ['in',$this -> sess['teacherIds']];
        }
        $groupIds = $this->auth->getGroupIds();
        //学院单位
        if($groupIds[0]==7)
        {
            $info = (new \app\admin\model\College) -> where('name',$this->auth->username) -> find();
            if(isset($info))
            {
                $departmentId = (new \app\admin\model\Department) -> where('college_id',$info['id']) -> column('id');
                $whereTea['department_id'] = ['in',$departmentId];
                $whereQues['college_id'] = $info['id'];
            }
        }

        $reported = (new \app\admin\model\QuesTeacher()) -> group('teacher_id') -> where($whereQues) -> where($this -> whereType) -> whereBetween('add_time',$timestampArr) -> column('teacher_id');

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            
            if(isset($filter['college_id']) && $filter['college_id']){
                $departmentId = (new \app\admin\model\Department) -> where('college_id',$filter['college_id']) -> column('id');
                $where_['department_id'] = ['in',$departmentId];
            }

            if(isset($filter['department_id'])){
                if(isset($where_['department_id'])) unset($where_['department_id']);
                $where_['department_id'] = ['=',$filter['department_id']];
            }


            $total = $this->model
                -> alias('a')
                -> where($whereTea)
                -> where($this -> whereType)
                -> whereNotIn('a.id',$reported)
                ->where($where_)
                ->join('department b','b.id=a.department_id')
                ->join('college c','c.id=b.college_id')
                ->field('nickname,tel,b.name as department_name ,c.name as college_name')
                ->order('a.id','desc')
                ->count();

            $list = $this->model
                -> alias('a')
                -> where($whereTea)
                -> where($this -> whereType)
                -> whereNotIn('a.id',$reported)
                ->where($where_)
                ->join('department b','b.id=a.department_id')
                ->join('college c','c.id=b.college_id')
                ->field('nickname,tel,b.name as department_name ,c.name as college_name')
                ->order('a.id','desc')
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }


        $allcount = (new \app\admin\model\Teacher()) -> alias('a') -> where($whereTea) ->where($this-> whereType) ->where($where_time)-> count();
        // $reportedcount = count($reported);
        // $notreportedallcount = $allcount - $reportedcount;
        $notreportedallcount = $this->model
            -> alias('a')
            -> where($whereTea)
            -> where($this -> whereType)
            -> whereNotIn('a.id',$reported)
            ->where($where_)
            ->join('department b','b.id=a.department_id')
            ->join('college c','c.id=b.college_id')
            ->field('nickname,tel,b.name as department_name ,c.name as college_name')
            ->order('a.id','desc')
            ->count();
        $reportedcount = $allcount - $notreportedallcount;
        
        $today = Time::today();
        $todayStr = date("Y-m-d H:i:s",$today[0]).' - '.date("Y-m-d H:i:s",$today[1]);
        if(isset($this ->sess['college_id'])){
            $this -> assign('collegestr','id='.$this -> sess['college_id']);
        }else{
            $this -> assign('collegestr','');
        }
        $this -> assign('today', $todayStr);
        $this -> assign('allcount',$allcount);
        $this -> assign('reportedcount',$reportedcount);
        $this -> assign('notreportedallcount',$notreportedallcount);
        return $this->view->fetch();
    }
}
